﻿/*CREATE VIEW dbo.VIEW_SEARCH_FOR_INV_OUT
AS
SELECT     dbo.PRODUCT_UNIT.ID, dbo.PRODUCT_UNIT.NAME, dbo.NSI_PRODUCT_STATE.NAME AS NSI_PRODUCT_STATE_NAME, dbo.PRODUCT_UNIT.TYPE, 
                      dbo.PRODUCT_COST.COST, SUM(dbo.PAYMENT_IN_ITEM.SUM_CUR) AS PAYMENT_IN_SUM_CUR, SUM(dbo.PAYMENT_IN_ITEM.SUM_RUB) 
                      AS PAYMENT_IN_SUM_RUB, dbo.NSI_CURRENCY.SHORT_NAME AS NSI_CURRENCY_NAME, 
                      dbo.COST_PROTOCOL.NUMBER AS COST_PROTOCOL_NUMBER, dbo.PRODUCT_UNIT.CP_STAGE_ID, 
                      dbo.CP_STAGE.NUMBER AS CP_STAGE_NUMBER, dbo.CP_STAGE.NAME AS CP_STAGE_NAME, 
                      dbo.CP_STAGE.START_DATE AS CP_STAGE_START_DATE, dbo.CP_STAGE.END_DATE AS CP_STAGE_END_DATE, dbo.PRODUCT_UNIT.PID, 
                      dbo.PRODUCT_UNIT.NSI_PRODUCT_STATE_ID
FROM         dbo.CP_STAGE INNER JOIN
                      dbo.COST_PROTOCOL INNER JOIN
                      dbo.PRODUCT_COST ON dbo.COST_PROTOCOL.ID = dbo.PRODUCT_COST.COST_PROTOCOL_ID INNER JOIN
                      dbo.PRODUCT_UNIT ON dbo.PRODUCT_COST.PRODUCT_UNIT_ID = dbo.PRODUCT_UNIT.ID ON 
                      dbo.CP_STAGE.ID = dbo.PRODUCT_UNIT.CP_STAGE_ID INNER JOIN
                      dbo.NSI_PRODUCT_STATE ON dbo.PRODUCT_UNIT.NSI_PRODUCT_STATE_ID = dbo.NSI_PRODUCT_STATE.ID INNER JOIN
                      dbo.NSI_CURRENCY ON dbo.COST_PROTOCOL.NSI_CURRENCY_ID = dbo.NSI_CURRENCY.ID LEFT OUTER JOIN
                      dbo.PAYMENT_IN_ITEM ON dbo.PRODUCT_UNIT.ID = dbo.PAYMENT_IN_ITEM.PRODUCT_UNIT_ID
GROUP BY dbo.PRODUCT_UNIT.CP_STAGE_ID, dbo.PRODUCT_UNIT.TYPE, dbo.PRODUCT_UNIT.NAME, dbo.PRODUCT_COST.COST, dbo.CP_STAGE.NUMBER, 
                      dbo.CP_STAGE.NAME, dbo.CP_STAGE.START_DATE, dbo.CP_STAGE.END_DATE, dbo.PRODUCT_UNIT.PID, dbo.COST_PROTOCOL.NUMBER, 
                      dbo.NSI_PRODUCT_STATE.NAME, dbo.PRODUCT_UNIT.NSI_PRODUCT_STATE_ID, dbo.PRODUCT_UNIT.ID, dbo.NSI_CURRENCY.SHORT_NAME*/